/**
 * =========================== 协议解析js ===========================
 */

/**不使用原生标题栏*/
function sendAppConfig(newPageOpen) {
  sendAppConfig2(newPageOpen, false, "");
}

/**使用原生标题栏*/
function sendAppConfig2(newPageOpen, showTitle, title) {
  var data = {
    "newPageOpen": newPageOpen,
    "showTitle": showTitle,
    "title": title,
    "fullScreen": false,
    "landscape": false,
    "path": window.location.pathname
  };
  setAppConfig(data);
}

function setAppConfig(config) {
  if (isAndorid()) {
    window.android.setConfig(JSON.stringify(config));
  } else if (isIOS()) {
    if (window.webkit != null)
      window.webkit.messageHandlers.setConfig.postMessage(JSON.stringify(config));
  } else {
    console.log("普通页面无法完成此操作");
  }
}

/**关闭当前页面*/
function goBack() {
  if (isAndorid()) {
    window.android.goBack();
  } else if (isIOS()) {
    if (window.webkit != null)
      window.webkit.messageHandlers.goBack.postMessage("");
  } else {
    history.go(-1);
  }
}


/**关闭页面根据页面相对名称*/
function closePageByName(name) {
  if (isAndorid()) {
    window.android.closePageByName(name);
  } else if (isIOS()) {
    if (window.webkit != null)
      window.webkit.messageHandlers.closePageByName.postMessage(name);
  } else {
    console.log("普通页面无法完成此操作");
  }

}

/**关闭所有页面除了传入的名称地址页面*/
function closePageExceptName(name) {
  if (isAndorid()) {
    window.android.closePageExceptName(name);
  } else if (isIOS()) {
    if (window.webkit != null)
      window.webkit.messageHandlers.closePageExceptName.postMessage(name);
  } else {
    console.log("普通页面无法完成此操作");
  }

}

/**关闭所有页面*/
function closeAllPage() {
  if (isAndorid()) {
    window.android.closeAllPage();
  } else if (isIOS()) {
    if (window.webkit != null)
      window.webkit.messageHandlers.closeAllPage.postMessage("");
  } else {
    console.log("普通页面无法完成此操作");
  }
}

/**关闭所有页面并新开一个页面*/
function closeAllAndOpenNewPage(path) {
  var url = getHost() + path;
  if (isAndorid()) {
    window.android.closeAllAndOpenNewPage(url);
  } else if (isIOS()) {
    if (window.webkit != null)
      window.webkit.messageHandlers.closeAllAndOpenNewPage.postMessage(url);
  } else {
    window.location.href = path;
  }
}

/**打开扫码器扫码,传入手动输入提示文本*/
function openCapture(handleHint) {
  if (isAndorid()) {
    window.android.openCapture(handleHint);
  } else if (isIOS()) {
    if (window.webkit != null)
      window.webkit.messageHandlers.openCapture.postMessage(handleHint);
  } else {
    console.log("普通页面无法完成此操作");
  }
}

/**拨打电话*/
function callPhone(phone) {
  if (isAndorid()) {
    window.android.callPhone(phone);
  } else if (isIOS()) {
    if (window.webkit != null)
      window.webkit.messageHandlers.callPhone.postMessage(phone);
  } else {
    console.log("普通页面无法完成此操作");
  }
}

/**复制到剪切板*/
function clipToBorad(content) {
  if (isAndorid()) {
    window.android.clipToBorad(content);
  } else if (isIOS()) {
    if (window.webkit != null)
      window.webkit.messageHandlers.clipToBorad.postMessage(content);
  } else {
    console.log("普通页面无法完成此操作");
  }
}

/**开始导航*/
function openGuide(addressName, lat, lng) {//地址名、维度、经度
  if (isAndorid()) {
    window.android.openGuide(addressName, lat, lng);
  } else if (isIOS()) {
    if (window.webkit != null)
      window.webkit.messageHandlers.openGuide.postMessage(addressName, lat, lng);
  } else {
    console.log("普通页面无法完成此操作");
  }
}

/**开始导航*/
function openChoiceLocation() {//地址名、维度、经度
  if (isAndorid()) {
    window.android.openChoiceLocation();
  } else if (isIOS()) {
    if (window.webkit != null)
      window.webkit.messageHandlers.openChoiceLocation.postMessage();
  } else {
    console.log("普通页面无法完成此操作");
  }
}

/**是否是ios设备*/
function isIOS() {
  var u = navigator.userAgent;
  var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
  return isiOS;
}

/**是否是android设备*/
function isAndorid() {
  // return (typeof window.android !== "undefined");
  var u = navigator.userAgent;
  return u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
}

/**当前环境是否在微信*/
function isInWechat() {
  var ua = navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == "micromessenger") {
    return true;
  } else {
    return false;
  }
}


/**是否是android设备*/
function getHost() {
  return window.location.protocol + "//" + window.location.host;
}

var onResumeListener;
var onCaptureListener;
var onLocationListener;


function setResumeListener(listener) {
  onResumeListener = listener;
}

function setCaptureListener(listener) {
  onCaptureListener = listener;
}

function setLocationListener(listener) {
  onLocationListener = listener;
}


/**页面恢复回调 app调用*/
function onResume() {
  if (onResumeListener != null) {
    onResumeListener();
  }
}

/**扫码回调 app调用*/
function onCapture(result) {
  if (onCaptureListener != null) {
    onCaptureListener(result);
  }
}

/**地址选择回调*/
function onLocationSelect(name, address, lat, lng) {
  if (onLocationListener != null) {
    onLocationListener(name, address, lat, lng);
  }
}

$(function () {
  $(".bo-header-left").click(function () {
    goBack();
  });
});
